package de.baumann.browser.fragments.duobao

import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.duobao.DuoBaoDetailActivity
import de.baumann.browser.activitys.duobao.DuoBaoWanFaGuiZeActivity.Companion.startDuoBaoWanFaGuiZeActivity
import de.baumann.browser.adapter.DuoBaoAdapter
import de.baumann.browser.api.net.vo.duobao.Banner
import de.baumann.browser.api.net.vo.duobao.DuoBao
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.iview.duobao.IDuoBaoHomeView
import de.baumann.browser.present.duobao.DuoBaoHomePresenter
import de.baumann.browser.units.GlideImageLoader
import de.baumann.browser.utils.EndTimeCallback
import kotlinx.android.synthetic.main.fragment_duo_bao_home.*

class DuoBaoHomeFragment : BaseOdinFragment<IDuoBaoHomeView, DuoBaoHomePresenter>(), IDuoBaoHomeView {

    companion object {
        val instance: DuoBaoHomeFragment by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            DuoBaoHomeFragment()
        }
    }

    private lateinit var duoBaoAdapter: DuoBaoAdapter

    override fun getLayout(): Int {
        return R.layout.fragment_duo_bao_home
    }

    override fun initView(view: View) {

        back.setOnClickListener {
            activity?.finish()
        }

        duobaoBanner.setImageLoader(GlideImageLoader())

        wfgz.setOnClickListener {
            startDuoBaoWanFaGuiZeActivity(mContext)
        }
    }

    override fun initData() {
        getPresenter().getNewMsg()
        getPresenter().getBanner()
    }

    override fun onResume() {
        super.onResume()
        getPresenter().getList()
    }

    override fun createPresenter(): DuoBaoHomePresenter? {
        return DuoBaoHomePresenter()
    }

    override fun createView(): IDuoBaoHomeView? {
        return this
    }

    override fun setBanners(list: List<Banner>) {
        duobaoBanner.setImages(list)
        duobaoBanner.start()
    }

    override fun setDuoBaoMsg(address: String) {
        duobaoMsg.text = "恭喜钱包地址结尾为${address}的用户中得大奖"
    }

    override fun setDuoBaoList(list: List<DuoBao>?) {
        duoBaoAdapter = DuoBaoAdapter(object :EndTimeCallback{
            override fun callback() {
                getPresenter().getList()
            }
        })

        duobaoliebiao.adapter = duoBaoAdapter

        duoBaoAdapter.setOnItemClickListener { _, _, position ->
            val duobao = duoBaoAdapter.getItem(position)
            DuoBaoDetailActivity.startDuoBaoDetailActivity(mContext, duobao!!.id.toString(), duobao.phaseNum)
        }
        duoBaoAdapter.setNewData(list)
    }

}